<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>力扣 宝石补给</title>
</head>
<body>

</body>
<script>
    const copy = (obj) => JSON.parse(JSON.stringify(obj))
    const eg = [
        [[3,1,2], [[0,2],[2,1],[2,0]], 2],
        [[100,0,50,100], [[0,2],[0,1],[3,0],[3,0]], 75],
        [[0,0,0,0], [[1,2],[3,1],[1,2]], 0],
        [[0,2,5,4], [[3,2],[3,2],[1,3],[0,2],[3,0],[3,1],[0,3],[2,1],[3,0]], 4]
    ]

    const f1 = (gem, operations) => {
        var giveGem1 = function(gem, operations) {
            operations.forEach(([x,y], index) => {
                const down = Math.floor(gem[x] / 2);
                gem[x] = gem[x] - down;
                gem[y] = gem[y] + down;
            });
            const max = Math.max(...gem);
            const min = Math.min(...gem);
            return max - min
        };
        const res1 = giveGem1(gem, operations);
        return res1
    }

    const f2 = (gem, operations) => {
        var giveGem2 = function (gem, operations) {

            operations.forEach(([x,y], index) => {
                const down = Math.floor(gem[x] / 2);
                gem[x] = gem[x] - down;
                gem[y] = gem[y] + down;

            });
            gem.sort((a, b) => b - a);
            return gem[0] - gem[gem.length - 1]
        }
        const res = giveGem2(gem, operations)
        return res
    }

    console.log('方法一')
    eg.forEach(([gem, operations, res], index) => {
        console.log(`第${index + 1}个实例`, f1(copy(gem), operations) === res ? '通过' : '不通过')
    })
    console.log('方法二')
    eg.forEach(([gem, operations, res], index) => {
        console.log(`第${index + 1}个实例`, f2(copy(gem), operations) === res ? '通过' : '不通过')
    })



</script>
</html>
